package com.xuecheng.media.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.xuecheng.api.content.AliyunVodApi;
import com.xuecheng.api.content.aliyun.VodUploadRequest;
import com.xuecheng.api.content.aliyun.VodUploadToken;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.media.common.constant.MediaErrorCode;
import com.xuecheng.media.common.utils.AliyunVODUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.annotation.XmlEnumValue;

/**
 * - @author by 姜暄煊
 * - @Date 12.14
 * - @Description
 */
@Slf4j
@RestController
public class AliyunVodController implements AliyunVodApi {

    @Value("${aliyun.region}")
    private String region;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    /**
     *
     * @param request 媒体上传请求信息封装类
     * @return   VodUploadToken
     */
    @PostMapping("media/vod-token")
    public VodUploadToken generateUploadToken(@RequestBody VodUploadRequest request) {
        //在我们的程序与第三方服务产生交互 必须要进行try-catch处理 因为网络问题

        try {
            //1.初始化客户端
            DefaultAcsClient client = AliyunVODUtil.initVodClient(region, accessKeyId, accessKeySecret);
            //2.创建请求对象并获取对象
            CreateUploadVideoResponse response = AliyunVODUtil.createUploadVideo(client, request.getTitle(), request.getFileName());
            //3.解析响应数据
            VodUploadToken token = new VodUploadToken();
            token.setVideoId(response.getVideoId());
            token.setRequestId(response.getRequestId());
            token.setUploadAuth(response.getUploadAuth());
            token.setUploadAddress(response.getUploadAddress());
            return token;
        } catch (Exception e) {
            //千万不能用e.printStackTrace();
            String message = e.getMessage();

            log.error(MediaErrorCode.E_140011.getDesc()+" : {}",message);

            ExceptionCast.cast(CommonErrorCode.UNKOWN);
            //在此因为try-catch无法识别这是一个异常信息 所以我们要return null 回去
            return null;
        }
    }

    @GetMapping("media/refresh-vod-token/{videoId}")
    public VodUploadToken refreshUploadToken(@PathVariable String videoId) {

        try {
            //初始化数据
            DefaultAcsClient client = AliyunVODUtil.initVodClient(region,accessKeyId,accessKeySecret);
            //创建请求对象并获取响应对象
            RefreshUploadVideoResponse response = AliyunVODUtil.refreshUploadVideo(client, videoId);
            //解析响应数据
            VodUploadToken token = new VodUploadToken();
            token.setVideoId(response.getVideoId());
            token.setUploadAuth(response.getUploadAuth());
            token.setUploadAddress(response.getUploadAddress());
            token.setRequestId(response.getRequestId());
            return token;
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(MediaErrorCode.E_140015.getDesc()+" : {}",message);

            // throw new BusinessException();
            // 编译器的问题：只看代码，不管代码中方法调用的结果内容
            ExceptionCast.cast(CommonErrorCode.UNKOWN);
            return null;
        }



    }
}
